#!/bin/bash

# handlebase.h
echo "
#ifndef _3761_HANDLE_BASE_H_
#define _3761_HANDLE_BASE_H_
#include <list>
#include <stdint.h>
#include <stdbool.h>
#include \"3761/3761.h\"
using namespace std;

#pragma pack(0)

class UserData
{
public:
	uint16_t cmd;
};

class GB3761_HandleBase
{
public:
	uint8_t user_buffer[1024];
	uint8_t frame_buffer[1024];
	uint16_t user_length;
	uint16_t frame_length;
	const uint8_t afn_type;
protected:
	list<UserData> ulist;

protected:
	GB3761_HandleBase(const uint8_t t);

public:
	bool push(UserData&);
	bool remove(uint8_t cmd);
	int parse(const uint8_t*);
	int make(uint8_t*);
	int make(UserData&, uint8_t*);
public:" > handlebase.h

#parseFxxx() loop
 ((i = 0))
while  ((i < 256 ))
do
	printf "\tvirtual int parseF%d(const uint8_t*);\n" $i >> handlebase.h
	((i++))
done
#parseFxxx() end

printf "public:" >> handlebase.h

#makeFxxx() loop
 ((i = 0))
while  ((i < 256 ))
do
	printf "\tvirtual int makeF%d(UserData&, uint8_t*);\n" $i >> handlebase.h
	((i++))
done
#makeFxxx() end


echo "
};
#endif//_3761_HANDLE_BASE_H_
" >> handlebase.h
######################################################


# handlebase.cpp
echo "
#include <list>
#include <stdio.h>
#include <string.h>
#include \"handlebase.h\"


GB3761_HandleBase::GB3761_HandleBase(uint8_t type)
:afn_type(type),user_length(0),frame_length(0)
{
	memset(user_buffer, 0, sizeof(user_buffer));
	memset(frame_buffer, 0, sizeof(frame_buffer));
}" > handlebase.cpp

#parseFxxx(){} loop(0~255)
 ((i = 0))
while  ((i < 256 ))
do
printf "
int GB3761_HandleBase::parseF$i(const uint8_t* data)
{
	printf(\"AFN%%02X.%%s\\\\n\", afn_type>>8, __func__);
	return 0;
}">> handlebase.cpp
 ((i++))
done
#parseFxxx(){} end

printf "//--------------------------------------------\n" >> handlebase.cpp

#makeFxxx(){} loop(0~255) 
 ((i = 0))
while  ((i < 256 ))
do
printf "
int GB3761_HandleBase::makeF$i(UserData& data, uint8_t* buf)
{
	printf(\"AFN%%02X.%%s\\\\n\", afn_type>>8, __func__);
	return 0;
}" >> handlebase.cpp
 ((i++))
done
#makeFxxx(){} end


#push()
#parse()
#make()
printf "bool GB3761_HandleBase::push(UserData& data)
{
	uint8_t t = (data.cmd >> 8);
	uint8_t f = (data.cmd & 0xff);
	if(afn_type != t)
	{
		return false;
	}
	ulist.push_back(data);
}

int GB3761_HandleBase::parse(const uint8_t *data)
{
	int len = 0;


	return len;
}

int GB3761_HandleBase::make(uint8_t *buf)
{
	int len = 0;

	for(list<UserData>::iterator i = ulist.begin(); i != ulist.end(); i++)
	{
		len += make(*i, buf + len);
	}
	return len;
}" >> handlebase.cpp

printf "
int GB3761_HandleBase::make(UserData& data, uint8_t* buf)
{
	switch(data->cmd & 0xff)
	{\n" >> handlebase.cpp

	((x=0))
	while ((x<256))
	do
		printf "\tcase $x: return makeF$x(data, buf);\n" >> handlebase.cpp
	((x++))
	done

printf "
	}
	return 0;
}" >> handlebase.cpp

